public class Solution419 {

    int[] dx=new int[]{1,0,-1,0};
    int[] dy=new int[]{0,1,0,-1};

    public int countBattleships(char[][] board) {
        int ans=0;
        int n=board.length;
        int m=board[0].length;
        int[][] map=new int[n][m];
        for (int i=0;i<n;i++){
            for (int j=0;j<m;j++){
                if (board[i][j]=='X' && map[i][j]==0){
                    dfs(board,map,i,j);
                    ans++;
                }
            }
        }
        return ans;
    }
    void dfs(char[][] board,int[][] map,int x,int y){
        if (x<0 || x>=board.length || y<0 || y>=board[0].length){
            return;
        }
        if (board[x][y]=='.' || map[x][y]==1){
            return;
        }
        map[x][y]=1;
        for (int i = 0; i < 4; i++) {
            dfs(board,map,x+dx[i],y+dy[i]);
        }
    }

    public static void main(String[] args) {
        System.out.println(new Solution419().countBattleships(new char[][]{{'X','.','.','X'},{'.','.','.','X'},{'.','.','.','X'}}));
    }
}
